/*
 * @(#)ListWorkflowAutomaticTaskAction.java
 *
 * Copyright (c) 2006 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowAction;
import com.dcivision.workflow.core.WorkflowAutomaticTaskManager;
import com.dcivision.workflow.dao.WorkflowActionDAObject;
/**
 * <p>Class Name:    ListWorkflowAutomaticTaskAction.java    </p>
  *
 * @author           tc //mod by greatwall shao
 * @company          DCIVision Limited
 * @creation date    2006-08-23
 */
public class ListWorkflowAutomaticTaskAction extends AbstractListAction {

  public String getFunctionCode() {
    
    return (SystemFunctionConstant.WORKFLOW_AUTOMATICTASK);
  }

  public String getMajorDAOClassName() {
    
    return ("com.dcivision.workflow.dao.WorkflowActionDAObject");
  }

  public String getListFunctionName() {
    return ("getCustomerWorkflowAutomaticTaskList");
  }

  public ListWorkflowAutomaticTaskAction() {
    super();
    this.setListName("automaticTaskList");
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {

    AbstractSearchForm searchForm = (AbstractSearchForm) form;
    log.info(this.getClass().getName() + ", OP Mode:" + searchForm.getOpMode() + ", Nav Mode:" + searchForm.getNavMode());
    //  Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward != null) {
      return forward;
    }
    if (GlobalConstant.OP_MODE_LIST_DELETE.equals(searchForm.getOpMode())) {
      WorkflowActionDAObject actionDAO = new WorkflowActionDAObject(this.getSessionContainer(request), this.getConnection(request));
      String[] selectedID = searchForm.getBasicSelectedID();

      for (int j = 0; j < selectedID.length; j++) {
      if(!Utility.isEmpty(selectedID[j])&&(actionDAO.isHasStep(Integer.valueOf(selectedID[j]))))
      {
        addError(request,"automatictask.error.delete.info");
        this.getListData(mapping, searchForm, request, response);
        return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
      }
      }
    }
    return super.execute(mapping, form, request, response);
  }

  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping, form, request, response);
  }

  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    WorkflowActionDAObject actionDAO = new WorkflowActionDAObject(this.getSessionContainer(request), this.getConnection(request));
    WorkflowAutomaticTaskManager automaticTaskManager = new WorkflowAutomaticTaskManager(this.getSessionContainer(request), this.getConnection(request));
    String[] selectedID = form.getBasicSelectedID();

    for (int i = 0; i < selectedID.length; i++) {
      
      if (!Utility.isEmpty(selectedID[i])) {
        // Delete src File AND BIN FILE
        WorkflowAction workflowAction = (WorkflowAction) actionDAO.getObjectByID(new Integer(selectedID[i]));
        String classname = workflowAction.getActionClassName();        
        classname = classname.replace('.', '/');
        automaticTaskManager.deleteSrcFile(classname + ".java");//delete src file
        automaticTaskManager.deleteBinFile(classname + ".class");//delete bin file
      }

    }
    super.deleteListData(mapping, form, request, response);
  }

}
